//
//  DWTimer.m
//  OCAPPDemo
//
//  Created by 杨冬武 on 2021/4/19.
//

#import "DWWeakTimerTool.h"

@interface DWWeakTimerTool ()
@property (nonatomic, weak) id aTarget;
@property (nonatomic, assign) SEL aSelector;
@end

@implementation DWWeakTimerTool

+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo {
    
    DWWeakTimerTool *proxy = [[DWWeakTimerTool alloc] init];
    proxy.aTarget = aTarget;
    proxy.aSelector = aSelector;
    
    return [NSTimer timerWithTimeInterval:ti target:proxy selector:@selector(updateTimer:) userInfo:userInfo repeats:yesOrNo];
    
}

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo {
    
    DWWeakTimerTool *proxy = [[DWWeakTimerTool alloc] init];
    proxy.aTarget = aTarget;
    proxy.aSelector = aSelector;
    
    return [NSTimer scheduledTimerWithTimeInterval:ti target:proxy selector:@selector(updateTimer:) userInfo:userInfo repeats:yesOrNo];
}

- (void)updateTimer:(NSTimer *)timer {
    
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    
    [self.aTarget performSelector:self.aSelector withObject:timer];
    
#pragma clang diagnostic pop
}

- (void)dealloc {
    NSLog(@"%s", __FUNCTION__);
}

@end
